package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.R;

public class TwoActivity extends AppCompatActivity {
    private ListView listView;
    private String[] classnameArray;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two);
        initView();
        initData();
        initEvent();
    }
    //视图
    public void initView(){
        listView = findViewById(R.id.listvieww);
    }
    //数据
    public void initData(){
        classnameArray = new String[]{"大数据","移动开发","艺术设计","软件技术"};
        listView.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return classnameArray.length;
            }

            @Override
            public Object getItem(int position) {
                return classnameArray[position];
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if (convertView ==null){
                    convertView = View.inflate(TwoActivity.this,R.layout.listview_item_layout,null);
                }
                TextView usernameTV = convertView.findViewById(R.id.username_tv);
                usernameTV.setText(classnameArray[position]+"");
                return convertView;
            }
        });
    }
    //事件
    public void initEvent(){
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //点击跳转到TwoActivity[数据展示信息学院20级专业班级]

                Toast.makeText(TwoActivity.this,classnameArray[position]+"", Toast.LENGTH_SHORT).show();
            }
        });
    }
}